#pragma strict

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var quitClicked : boolean;
var isUserSure : boolean;
var fadeTexture : Texture;
var fadeTime : float;
var alphaFadeValue : float;
var hasStarted : boolean;
var levelLoad : boolean;

private var moveDirection : Vector3 = Vector3.zero;

function Start(){
	
	alphaFadeValue = 1;
	fadeTime = 2;
	hasStarted = false;
	levelLoad = true;
	
}

function Update () {

	if(hasStarted){
		var controller : CharacterController = GetComponent(CharacterController);
		if(controller.isGrounded){
			moveDirection = Vector3(Input.GetAxis("Horizontal"),0,0);
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;
		
			if(Input.GetButton("Jump")){
				moveDirection.y = jumpSpeed;
			}
		}
	
		moveDirection.y -= gravity * Time.smoothDeltaTime;
		controller.Move(moveDirection * Time.smoothDeltaTime);
		transform.position.z = 0;
	
		if(isUserSure){
			Application.LoadLevel("MainMenu");
		}
		
	}
	
}

function OnGUI(){

	if(levelLoad){
		alphaFadeValue -= Mathf.Clamp01(Time.deltaTime / fadeTime);
		
		GUI.color = Color(0,0,0,alphaFadeValue);
		GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),fadeTexture);
		
		if(alphaFadeValue <= 0){
			hasStarted = true;
			fadeTime = 0;
			alphaFadeValue = 0;
			levelLoad = false;
		}
	}

	GUILayout.BeginArea(Rect(10,10,95,20));
	GUILayout.BeginHorizontal();
	if(GUILayout.Button("Quit Game")){
		GameObject.Find("Main Camera").GetComponent(CameraScript).menuClicked = true;
	}	
	GUILayout.EndHorizontal();
	GUILayout.EndArea();

}

@script RequireComponent(CharacterController)